package com.googlecode.curvedmetromaps.core.logic.localsearch.geneticalgorithm;

import java.util.Collections;

public class Selection {

	private static final float survivalRate = 0.5f;
	
	/**
	 * Orders organisms based on fitness (cost) from good to bad. Copies some
	 * good organisms over bad organisms.
	 * @param population Population to perform selection on.
	 */
	public static void selection(Population population) {
		// Sort from good (low cost) to bad (high cost)
		Collections.sort(population.organisms);
		
		int survivers = Math.round(survivalRate * population.organisms.size());
		// Prevent 0% survival
		if (survivers == 0) survivers = 1;
		
		for (int i = survivers; i < population.organisms.size(); i++) {
			Organism source = population.organisms.get(i - survivers);
			population.organisms.set(i, new Organism(source));
		}
	}

}
